#ifndef PLANET_H
#define PLANET_H
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;

const int BUF_L = 100;

class Planet {
private:
    char* name;
    int diam;
    bool life;
    int sat;
    static int total;
    int id;
public:
    Planet(){
        this->name = nullptr;
        this->diam = 0;
        this->life = false;
        this->sat = 0;
        ++total;
        id = total;
        cout << "Создание ID " << id << endl;
    };
    Planet(char* name, int diam, bool life, int sat){
        int l_name = strlen(name) + 1;
        this->name = new char[l_name];
        strncpy(this->name, name, l_name);
        this->diam = diam;
        this->life = life;
        this->sat = sat;
        ++total;
        id = total;
        cout << "Создание ID " << id << endl;
    }
    ~Planet(){
        cout << "Удаление " << id << endl;
        delete[] name;
        --total;
    };
    const char* getName(){
        return name;
    }
    int getDiam(){
        return diam;
    }
    bool getLife(){
        return life;
    }
    int getSat(){
        return sat;
    }
    void setName(){
                        char buf[BUF_L];
                cout << "Введите новое имя\n";
                cin >> buf;
                int l_name = strlen(buf) + 1;
                delete[] name;
                name = new char[l_name];
                strncpy(name, buf, l_name);
    }
    void setDiam(){
                        int bufDiam;
                cout << "Введите новый диаметр\n";
                cin >> bufDiam;
                diam = bufDiam;
    }
    void changeLife(){
                        if (life==false) {
                    life=true;
                    cout << "Вы создали жизнь!\n";
                }
                else
                {
                    life=false;
                    cout << "Вы уничтожили жизнь!\n";
                }
    }
    void setSat(){
                        int bufSat;
                cout << "Введите новый диаметр\n";
                cin >> bufSat;
                sat = bufSat;
    }
   Planet &operator=(const Planet &other) {
        if (this != &other) {
            delete[] name;
        int l_name = strlen(other.name) + 1;
        name = new char[l_name];
        strncpy(name, other.name, l_name);
        diam = other.diam;
        life = other.life;
        sat = other.sat;
        }
        return *this;
 }
    bool operator>(const Planet &other) {
        return this->diam > other.diam;
        }
    bool operator==(const Planet &other) {
        return this->diam == other.diam;
        }
    void edit(){
        print();
        cout << "Что вы хотите поменять?(1 для имени, 2 для диаметра, 3 для жизни, 4 для кол-ва спутников\n";
        int integer;
        cin >> integer;
        switch(integer){
            case 1: {
                setName();
                break;
            }
            case 2: {
                setDiam();
                break;
            }
            case 3: {
                changeLife();
                break;
            }
            case 4: {
                setSat();
                break;
            }
            default: cout << "Ничего не хочешь значит?\n";
                break;
            }
        }
    
    void print(){
       cout << "Имя: " << getName() << "\t" << "Диаметр: " << getDiam() << "\t" << "Жизнь: " << getLife() << "\t"<< "Количество спутников: " << getSat() << endl;
  };
};
ofstream &operator<<(ofstream &out, Planet &planet);
ifstream &operator>>(ifstream &in, Planet &planet);
int menu();
void read_database(const char *& filename, Planet *& planets, int & planets_count);
void write_database(const char *& filename, Planet *& planets, int & planets_count);
void sort_database(Planet *& planets, const int & planets_count); // 
void add(Planet *& planets, int &planets_count);
void remove(Planet *& planets, int & planets_count, const int & id);
void print_database(Planet *& planets, const int &planets_count);
int find(Planet *& planets, const int & planets_count);
#endif